<!DOCTYPE html>
<html>
<head>
	<title>Cooking Pressure Table</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
	<link rel="stylesheet" type="text/css" href="system/styles/igrX.css" />
<script type="text/javascript" src="system/scripts/igrX.js"></script>
</head>
<style>
	h2{
		text-align:center;
		font-size: 1.5em;
	}
	#content-table{
		width:100%;
		text-align: center;
	}
	#content-table th{
		text-align: center;
		font-weight: bold;
		background-color: #aaa;
	}
	#content-table tr:nth-child(even){
		background-color: #ddd;
	}
	
	#content-table tr:nth-child(odd){
		background-color: #eee;
	}
</style>
<h2>压力值大于平均值   Daily list of Cooking Pressure values greater than the average</h2>
<br />
<br />
<div>
	<span>Date: <input type="text" id="datep"></input></span>
	<p>Average Value: <span id="avg"></span></p>
</div>
<div>
<table id="content-table">
	<tr>
		<th>Value</th>
		<th>Time Stamp</th>
	</tr>
</table>
</div>

<script type="text/javascript">
	var updateTable = function(date) {
		var param = {};
		var date2 = new Date;
		var dateS = "";
		var date2S = "";
		
		// Table for pressure_cook in DB
		param["tbl"] = "log_14";
		
		// Empty table in browser display for writeback
		$("#content-table")['html']("");
		$("#content-table")['append']("<tr><th>Value</th><th>Time Stamp</th></tr>");
		
		// Prepare dates to send to SQL statement as parameters
		date2.setTime(Date.parse(date));
		dateS = (date2.getMonth() + 1) + "-" + (date2.getDate()) + "-" + date2.getFullYear();
		date2S = (date2.getMonth() + 1) + "-" + (date2.getDate() + 1) + "-" + date2.getFullYear();
		
		// Get average value first by querying "avgval" statement
		runSql("avgval", {tbl:"log_14",col:"nvalue"},{outfmt:"json"},function(result){
			if (result.statusCode == 200) {
				var avgval = JSON.parse(result.response)[0]['expr1000'];
				$("#avg")['html'](avgval); // Display average value on screen
				
				// Store all parameters in the param object to be send to another SQL statement "pcookgta"
				param["ts"] = dateS;
				param["ts2"] = date2S;
				param["val"] = avgval;
				runSql("pcookgta", param, {outfmt:"json"}, function(result) {
					if (result.statusCode == 200) {
						var data = JSON.parse(result.response);
						var tr = "";
						
						// Fill in the display table with data retrieved from the ran SQL statement
						for (var i in data){
							tr = "<tr>";
							tr += "<td>" + data[i]["nvalue"] + "</td>";
							tr += "<td>" + data[i]["time_stamp"] + "</td></tr>";
							$("#content-table")['append'](tr);
						}
					}
				});
			}
		});
	};
	//*/
	
	$(document).ready(function() {
		$("#datep")['datepicker']();
		$("#datep").datepicker("option", "dateFormat", "yy-mm-dd");
		$("#datep").change(function() {
			updateTable(this.value);
		});
	});
</script>
</html>